package de.carcophan.jimdb.gwt.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.ui.Image;

import de.carcophan.jimdb.gwt.client.service.LoginService;
import de.carcophan.jimdb.gwt.client.service.LoginServiceAsync;
import de.carcophan.jimdb.gwt.client.service.MovieService;
import de.carcophan.jimdb.gwt.client.service.MovieServiceAsync;
import de.carcophan.jimdb.gwt.client.util.WidgetUtil;

public class ClientFactoryImpl implements ClientFactory {

	private final JimdbClientBundle clientBundle = GWT
			.create(JimdbClientBundle.class);

	private final MovieServiceAsync movieService = GWT
			.create(MovieService.class);

	private final LoginServiceAsync loginService = GWT
			.create(LoginService.class);

	private final EventBus eventBus = new SimpleEventBus();

	private Image image;

	private WidgetUtil widgetUtil;

	@Override
	public MovieServiceAsync getMovieService() {
		return movieService;
	}

	@Override
	public LoginServiceAsync getLoginService() {
		return loginService;
	}

	@Override
	public EventBus getEventBus() {
		return eventBus;
	}

	@Override
	public JimdbClientBundle getClientBundle() {
		return clientBundle;
	}

	@Override
	public Image getAjaxLoadingImage() {
		if (image == null) {
			image = new Image(clientBundle.ajaxLoaderImage().getSafeUri());
		}
		return image;
	}

	@Override
	public WidgetUtil getWidgetUtil() {
		if (widgetUtil == null) {
			widgetUtil = new WidgetUtil(this);
		}
		return widgetUtil;
	}
}
